package com.xhd.db;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.math.BigDecimal;

import com.xhd.base.log.Log;
import com.xhd.base.log.LogFactory;
import com.xhd.base.util.Date;
import com.xhd.base.util.xBaseObj;
import com.xhd.config.BizObjClass;
import com.xhd.exception.IllegalDataException;
import com.xhd.exception.XException;
import com.xhd.util.DataRelation;

public class BizObjProxy {
	private xBaseObj jm114 = null;
	  private BizObjClass jm115 = null;
	  private static final Log jm9;
	  static Class jm10;

	  static
	  {
	    Class tmp3_0 = jm10;
	    if (tmp3_0 == null)
	    {
	     
	      try
	      {
	        BizObjProxy.jm10 = Class.forName("com.sinocc.base.config.BizObjProxy");
	      }
	      catch (ClassNotFoundException localClassNotFoundException)
	      {
	        throw new NoClassDefFoundError(localClassNotFoundException.getMessage());
	      }
	    }
	    jm9 = LogFactory.getLog(tmp3_0);
	  }

	  public BizObjProxy(xBaseObj paramxBaseObj)
	  {
	    this.jm114 = paramxBaseObj;
	    this.jm115 = BizObjClass.getInstance(paramxBaseObj.getClass());
	  }

	  public xBaseObj getBizObj()
	  {
	    return this.jm114;
	  }

	  public BizObjClass getBizClass()
	  {
	    return this.jm115;
	  }

	  public Object getValue(String paramString)
	  {
	    Field localField = this.jm115.getField(paramString);
	    if (localField == null)
	      return null;
	    try
	    {
	      return localField.get(this.jm114);
	    }
	    catch (Exception localException)
	    {
	    }
	    return null;
	  }

	  public String strFieldValue(String paramString)
	  {
	    Field localField = this.jm115.getField(paramString);
	    if (localField == null)
	      return null;
	    return strFieldValue(localField);
	  }

	  public String strFieldValue(Field paramField)
	  {
	    try
	    {
	      if (paramField.getClass().isArray())
	        return null;
	      if (Modifier.isFinal(paramField.getModifiers()))
	        return null;
	      String str = paramField.getType().getName();
	      Object localObject;
	      if (str.endsWith(".String"))
	      {
	        localObject = paramField.get(this.jm114);
	        return ((localObject == null) ? "" : localObject.toString());
	      }
	      if (str.equals("int"))
	        return String.valueOf(paramField.getInt(this.jm114));
	      if (str.endsWith(".BigDecimal"))
	      {
	        localObject = paramField.get(this.jm114);
	        return ((localObject == null) ? "0.0" : localObject.toString());
	      }
	      if (str.endsWith(".Date"))
	      {
	        localObject = paramField.get(this.jm114);
	        if (localObject == null)
	          return "";
	        return new Date(((java.util.Date)localObject).getTime()).toText();
	      }
	      return null;
	    }
	    catch (Exception localException)
	    {
	      jm9.error(localException);
	    }
	    return null;
	  }

	  public boolean setFieldValue(String paramString1, String paramString2)
	    throws IllegalDataException
	  {
	    Field localField = this.jm115.getField(paramString1);
	    if (localField == null)
	      return false;
	    return setFieldValue(localField, paramString2);
	  }

	  public boolean setFieldValue(Field paramField, Object paramObject)
	    throws IllegalDataException
	  {
	    if (paramField.getClass().isArray())
	      return false;
	    if (Modifier.isFinal(paramField.getModifiers()))
	      return false;
	    String str1 = paramField.getType().getName();
	    try
	    {
	      String str2 = null;
	      if (paramObject != null)
	        str2 = paramObject.toString();
	      if (str1.endsWith(".String"))
	      {
	        paramField.set(this.jm114, str2);
	      }
	      else if (str1.equals("int"))
	      {
	        if ((str2 == null) || (str2.length() < 1))
	          paramField.setInt(this.jm114, 0);
	        else
	          paramField.setInt(this.jm114, Integer.parseInt(str2));
	      }
	      else if (str1.endsWith(".BigDecimal"))
	      {
	        if ((str2 == null) || (str2.length() < 1))
	          paramField.set(this.jm114, new BigDecimal(0.0D));
	        else
	          paramField.set(this.jm114, new BigDecimal(str2));
	      }
	      else if (str1.endsWith(".Date"))
	      {
	        if ((str2 != null) && (str2.length() > 0))
	          paramField.set(this.jm114, new Date(str2));
	        else
	          paramField.set(this.jm114, null);
	      }
	      else if (str1.endsWith(".DataRelation"))
	      {
	        DataRelation localDataRelation = (DataRelation)paramField.get(this.jm114);
	        localDataRelation.setRelationData(paramObject);
	      }
	      else
	      {
	        return false;
	      }
	      return true;
	    }
	    catch (Throwable localThrowable)
	    {
	      jm9.error(localThrowable);
	      throw new IllegalDataException(10063, this.jm115.getFullName(), paramField.getName(), paramObject.toString());
	    }
	  }

	  public Object getEnv(String paramString)
	    throws XException
	  {
	    if ('@' == paramString.charAt(0))
	      paramString = paramString.substring(1);
	    if (paramString.startsWith("s:"))
	      return this.jm114.getSysConf2(paramString).objValue();
	    if (paramString.startsWith("u:"))
	      return this.jm114.getUserInfo().getProperty(paramString.substring(2));
	    return null;
	  }
}
